//
//  CourseManager.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/29.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "CourseManager.h"

static CourseManager *_manager = nil;
@interface CourseManager () {}
@property (nonatomic, strong) NSArray <Model_Course *>*dataArray;
@end
@implementation CourseManager
+ (CourseManager *)manager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[CourseManager alloc] init];
    });
    return _manager;
}

- (instancetype)init {
    if (self = [super init]) {
        [self initCourseData];
    }
    return self;
}
- (void)initCourseData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"CourseList" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:arr.count];
    for (NSDictionary *dic in arr) {
        Model_Course *model = [[Model_Course alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [mArr addObject:model];
    }
    self.dataArray = [NSArray arrayWithArray:mArr];
}
/**
 通过讲师id获取讲师课程列表
 
 @param lecturerId lecturerid
 @return 课程列表
 */
- (NSArray <Model_Course *>*)filterCourseWithLecturerId:(NSString *)lecturerId {
    
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        return [[(Model_Course *)evaluatedObject user_id] isEqualToString:lecturerId];
    }];
    NSArray *arr = [self.dataArray filteredArrayUsingPredicate:predicate];
    return arr;
}

/**
 通过课程标签获取讲师列表
 
 @param courseLabel courseLabel
 @return 课程标签
 */
- (NSArray <Model_Course *>*)filterCourseWithCourseLabel:(NSString *)courseLabel {
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        NSArray *arr = [(Model_Course *)evaluatedObject course_label];
        return [arr containsObject:courseLabel];
    }];
    NSArray *arr = [self.dataArray filteredArrayUsingPredicate:predicate];
    return arr;
}
@end
